package com.bourse.dbfeeder.services.feeder;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.mail.MessagingException;

import org.apache.log4j.Logger;
import org.joda.time.DateMidnight;

import com.bourse.core.services.cotation.CotationDataService;
import com.bourse.core.services.cotation.IndiceCompoDataService;
import com.bourse.core.services.utils.MailService;
import com.bourse.core.utils.Cotation;
import com.bourse.core.utils.IndiceComposition;
import com.bourse.core.utils.IsinCode;

public class FeederServiceImpl implements FeederService {

	/** The logger */
	private static final Logger LOGGER = Logger.getLogger(FeederServiceImpl.class);

	/** The histo data loader */
	private DataLoaderService histoDataLoader;

	/** The euronextDataLoader */
	private DataLoaderService internetDataLoader;

	/** The cotation data service*/
	private CotationDataService cotationDataService;

	/** The indice compo data service */
	private IndiceCompoDataService indiceCompoDataService;

	/** The mail service */
	private MailService mailService;
	

	/**
	 * @see FeederService#feedDatabase(FeederType)
	 */
	public void feedDatabase(FeederType feederType) {
		
		Map<IsinCode, List<Cotation>> cotations = new HashMap<IsinCode, List<Cotation>>(0);
		Map<IsinCode, List<IndiceComposition>> composition = new HashMap<IsinCode, List<IndiceComposition>>(0);
	
		try {
			/** data download */
			if (FeederType.HISTO.equals(feederType)) {

				LOGGER.info("[DbFeeder] - Loading data from histo file");
				cotations = histoDataLoader.getCotations();
				composition = histoDataLoader.getIndexComposition();

			} else if (FeederType.EURONEXT.equals(feederType)) {

				LOGGER.info("[DbFeeder] - Loading data from histo file");
				DateMidnight today = new DateMidnight();
				int day = today.getDayOfWeek();
				if (day != 6 && day != 7) {
					cotations = internetDataLoader.getCotations();
					composition = internetDataLoader.getIndexComposition();
				} else {
					LOGGER.info("[DbFeeder] - data not downloaded during the week");
				}

			}

			/** Data insertion */
			LOGGER.info("[DbFeeder] - Inserting cotation data to database");
			for (IsinCode isin : cotations.keySet()) {
				List<Cotation> cotationList = cotations.get(isin);
				cotationDataService.insertCotation(cotationList);
			}
			LOGGER.info("[DbFeeder] - Inserting cotation data to database DONE");
			LOGGER.info("[DbFeeder] - Inserting index composition data to database");
			for (IsinCode isinIndice : composition.keySet()) {
				List<IndiceComposition> compoList = composition.get(isinIndice);
				indiceCompoDataService.insertIndiceComposition(compoList);
			}
			LOGGER.info("[DbFeeder] - Inserting index composition data to database DONE");
		} catch (Exception e) {
			LOGGER.error("[DbFeeder] - GENERAL ERROR: " + e.getCause());
		}

		/** Send application execution status on */ 
		try{
			mailService.sendDbFeederLogStatus();
		}catch(MessagingException e){
			LOGGER.error("[DbFeeder] - ERROR while sending mail for application status");
		}
	}

	/**
	 * @param histoDataLoader
	 *            the histoDataLoader to set
	 */
	public void setHistoDataLoader(DataLoaderService histoDataLoader) {
		this.histoDataLoader = histoDataLoader;
	}

	/**
	 * @param internetDataLoader
	 *            the internetDataLoader to set
	 */
	public void setInternetDataLoader(DataLoaderService internetDataLoader) {
		this.internetDataLoader = internetDataLoader;
	}

	/**
	 * @param cotationDataService
	 *            the cotationDataService to set
	 */
	public void setCotationService(CotationDataService cotationDataService) {
		this.cotationDataService = cotationDataService;
	}

	/**
	 * @param indexCompoService
	 *            the indexCompoService to set
	 */
	public void setIndiceCompoService(IndiceCompoDataService indiceCompoDataService) {
		this.indiceCompoDataService = indiceCompoDataService;
	}

	/**
	 * @param mailService
	 *            the mailService to set
	 */
	public void setMailService(MailService mailService) {
		this.mailService = mailService;
	}

}
